<?php

namespace App\Repositories;

use App\Models\CategoryModel;

class CategoryRepository extends BaseRepository
{

    public function getParentCategory() {
        return CategoryModel::query()->where('pid',0)->get();
    }

    /**
     * @param $params
     * @return array[]
     */
    public function getFilter ($params) {
        $where = [];
        $whereBetween = [];
        if (!empty($params['period'])) {
            $where[] = [
                'period', $params['period']
            ];
        }
        if (!empty($params['category'])) {
            $where[] = [
                'category', 'like', '%' . $params['category'] . '%'
            ];
        }
        return [
            'where' => $where,
            'whereBetween' => $whereBetween
        ];
    }

    /**
     * @param $params
     * @return array
     */
    public function getList ($params)
    {
        $filter = $this->getFilter($params);
        $list = $this->getListItems(CategoryModel::query(), $filter, $params['page_size'] ?? 2000, ['*'], 'page', [
            'is_top' => 'desc',
            'sort' => 'desc'
        ]);
        return $list;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function save ($params)
    {
        return @$this->saveOrUpdate('CategoryModel', $params);
    }

}
